package com.applink.sell_proof.builder;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.applink.sell_proof.BasePagerActivity;
import com.applink.sell_proof.QuizDetail;
import com.applink.sell_proof.QuizExpiredDetail;
import com.applink.sell_proof.QuizResult;
import com.applink.sell_proof.R;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.IntentKey;
import com.applink.sell_proof.model.Quiz;

public class QuizBuilder extends AbstractListBuilder implements OnItemClickListener {

	public QuizBuilder(Context context, ViewGroup parent) {
		super(context, parent);
		((ListView) parent.findViewById(android.R.id.list)).setOnItemClickListener(this);
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		int amount_header = ((ListView) parent).getHeaderViewsCount();
		Quiz quiz = (Quiz) mData.get(position - amount_header);
		Intent intent;
		if (System.currentTimeMillis() > quiz.getEndDate()) {
			if (quiz.isSubmitted()) {
				intent = new Intent(mContext, QuizResult.class);
				intent.putExtra(Constant.KEY_QUIZ, quiz);
			} else {
				intent = new Intent(mContext, QuizExpiredDetail.class);
			}
		} else {
			if (quiz.isSubmitted()) {
				intent = new Intent(mContext, QuizExpiredDetail.class);
			} else {
				intent = new Intent(mContext, QuizDetail.class);
			}
		}
		int indexActivity = ((BasePagerActivity) mContext).getActivityIndex();
		intent.putExtra(IntentKey.ACTIVITY_NAME, indexActivity);
		intent.putExtra(Constant.KEY_QUIZ_ID, quiz.getId());
		mContext.startActivity(intent);
	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.quiz_item, null);
		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.linear_quiz_item, base.findViewById(R.id.linear_quiz_item));
		views.put(R.id.quiz_name, base.findViewById(R.id.quiz_name));
		views.put(R.id.quiz_end_date, base.findViewById(R.id.quiz_end_date));
		views.put(R.id.quiz_descripstion, base.findViewById(R.id.quiz_descripstion));
		views.put(R.id.quiz_stage, base.findViewById(R.id.quiz_stage));

		return views;
	}

	@Override
	public void builConvertViewContentData(int position, ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Quiz quiz = (Quiz) mData.get(position);
		((TextView) views.get(R.id.quiz_name)).setText(quiz.getName());

		String endDate = DateFormat.getDateFormat(mContext).format(new Date(quiz.getEndDate()));
		((TextView) views.get(R.id.quiz_end_date)).setText(endDate);

		((TextView) views.get(R.id.quiz_descripstion)).setText(quiz.getDescription());
		System.out.println("date 1 = "  + System.currentTimeMillis());
		System.out.println("date 2 = "  + quiz.getEndDate());

		if (quiz.isSubmitted()) {
			((TextView) views.get(R.id.quiz_stage)).setText(mContext.getString(R.string.quiz_participated));
			((LinearLayout) views.get(R.id.linear_quiz_item)).setBackgroundColor(mContext.getResources().getColor(R.color.OK));			
		} else if (System.currentTimeMillis() > quiz.getEndDate()) {
			((TextView) views.get(R.id.quiz_stage)).setText(mContext.getString(R.string.quiz_close));
			((LinearLayout) views.get(R.id.linear_quiz_item)).setBackgroundColor(mContext.getResources().getColor(R.color.PENDING));
		} else {
			((TextView) views.get(R.id.quiz_stage)).setText(mContext.getString(R.string.quiz_open));
			((LinearLayout) views.get(R.id.linear_quiz_item)).setBackgroundColor(mContext.getResources().getColor(R.color.PENDING));
		}
	}
}
